package com.tdtxdcxm.ruralcommunity.providers;

import com.tdtxdcxm.ruralcommunity.ResourceTable;
import com.tdtxdcxm.ruralcommunity.items.LCArryListContentItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.ArrayList;

public class LCProviderForContent extends BaseItemProvider {
    public ArrayList<LCArryListContentItem> list;
    private AbilitySlice slice;


    public LCProviderForContent(ArrayList<LCArryListContentItem> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()){
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cmp;
        if(component == null){
            cmp = LayoutScatter
                    .getInstance(slice.getContext())
                    .parse(ResourceTable.Layout_listcontainer_item_showtext, null,false);
        }
        else{
            cmp = component;
        }
        LCArryListContentItem lcarryListContentItem = list.get(i);
        Text typetext = (Text) ((DirectionalLayout)cmp).getComponentAt(0);
        typetext.setText(lcarryListContentItem.getType());

        Text contenttext = (Text) ((DirectionalLayout)cmp).getComponentAt(1);
        contenttext.setText(lcarryListContentItem.getDetail());

        DirectionalLayout messagedl = (DirectionalLayout) ((DirectionalLayout)cmp).getComponentAt(2);
        Text datetext = (Text) messagedl.getComponentAt(0);
        datetext.setText(lcarryListContentItem.getDate());

        Text field_idtext = (Text) messagedl.getComponentAt(1);
        field_idtext.setText(lcarryListContentItem.getField_id());

        Text readvaluetext = (Text) messagedl.getComponentAt(2);
        readvaluetext.setText(lcarryListContentItem.getReadvalue());


        return cmp;
    }
}
